package com.youhome.business.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.model.Principal;
import com.youhome.service.service.BSendRecordSMSGatherService;
import com.youhome.service.service.BTemplateSMSService;
import com.youhome.service.service.SMSService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2017/7/17.
 */

@RestController
@Api(tags = "短信")
public class SMSController {

    @Autowired
    private BTemplateSMSService bTemplateSMSService;
    @Autowired
    private SMSService smsService;
    @Autowired
    private BSendRecordSMSGatherService bSendRecordSMSGatherService;

    @ApiOperation(value = "获取验证短信", notes = "获取验证短信", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "type", value = "短信类型 注册:register,登陆:login,客户:client", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "phone", value = "手机号", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组ID", paramType = "query")
    })
    @GetMapping(value = "/sms/code/{type}/{phone}")
    public Object getSMS(@PathVariable String type,
                         @PathVariable String phone, String shareId) throws CustomException {
        return new Result(ResultCode.SUCCESS, smsService.getSMSCode(type, phone, shareId));
    }

    @ApiOperation(value = "获取短信模版列表", notes = "获取短信模版列表", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "type", value = "短信模版类型（1为验证码类，2为通知类，3为营销类，空为所有模版）", paramType = "query")
    })
    @GetMapping(value = "/sms/template/list")
    public Object templateList(Integer type) throws CustomException {
        return new Result(ResultCode.SUCCESS, bTemplateSMSService.selectAll(type));
    }

    @ApiOperation(value = "获取短信模版列表", notes = "获取短信模版列表", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "smss", value = "SMSS", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "tempId", value = "短信模版ID", required = true, paramType = "query")
    })
    @PostMapping(value = "/sms/send2")
    public Object sendSMS2(String smss, Integer tempId, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        smsService.send(principal, tempId, smss);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "短信发送历史记录列表", notes = "短信发送历史记录列表", response = ResultPage.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "当前页", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query")
    })
    @GetMapping(value = "/sms/history/list")
    public Object historyList(Integer pageNumber, Integer pageSize,
                              @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new ResultPage(ResultCode.SUCCESS, bSendRecordSMSGatherService.selectByBean(principal, pageNumber, pageSize));
    }

    @ApiOperation(value = "短信发送历史记录详情", notes = "短信发送历史记录详情", response = ResultPage.class)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "当前页", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "gatherId", value = "记录集Id", required = true, paramType = "query")
    })
    @GetMapping(value = "/sms/history/detail")
    public Object historyDetail(Integer pageNumber, Integer pageSize, Long gatherId, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new ResultPage(ResultCode.SUCCESS, smsService.selectByBean(principal, gatherId, pageNumber, pageSize));
    }
}
